package com.felix.sample;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.felix.sample.websocket.LatestDealsAndOutlets;
import com.felix.socketiolibrary.WebSocketHelper;
import com.felix.socketiolibrary.bean.DataAssembleHelper;
import com.google.gson.Gson;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private WebSocketHelper.OnDataReceiveListener mOnDataReceiveListener;

    private String[] symbols = {"BTC_USDT","ETH_USDT","TOK_ETH"};
    private int index = 0;
    private String mSymbolByUnderline = symbols[index];
    private TextView tvInfo;
    private ScrollView scrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_emit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emitData(mSymbolByUnderline);
            }
        });
        findViewById(R.id.btn_register_listener).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnDataReceiveListener = new WebSocketHelper.OnDataReceiveListener() {
                    @Override
                    public void onDataReceive(Object obj) {
                        Gson gson = new Gson();
                        LatestDealsAndOutlets latestDealsAndOutlets = gson.fromJson(obj.toString(), LatestDealsAndOutlets.class);
                        onReceiveDataSuccess(latestDealsAndOutlets);
                    }
                };
                WebSocketHelper.getInstance().registerDataReceiveListener(mOnDataReceiveListener);
            }
        });
        findViewById(R.id.btn_switch_tradepair).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSymbolByUnderline = symbols[(++index)%symbols.length];
                emitData(mSymbolByUnderline);
            }
        });
        tvInfo = findViewById(R.id.tv_info);
        scrollView = findViewById(R.id.scrollView);
    }

    private void setInfo(String info) {
        String oldInfo = tvInfo.getText().toString().trim();
        final StringBuilder sb = new StringBuilder(oldInfo);
        sb.append(TextUtils.isEmpty(oldInfo) ? "" : "\n").append(info);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvInfo.setText(sb);
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                    }
                });
            }
        });
    }

    public void emitData(String symbol) {
        if (WebSocketHelper.getInstance().isConnected()) {
            JSONObject jsonObject = DataAssembleHelper.getInstance().genEmitMessgeJson(symbol);
            WebSocketHelper.getInstance().emitData(jsonObject);
        } else {
            WebSocketHelper.getInstance().connect();
        }
    }

    public void onReceiveDataSuccess(LatestDealsAndOutlets latestDealsAndOutlets) {
        String symbolId = latestDealsAndOutlets.getSymbolId();
        LogUtils.i("symbolId = " + symbolId + ",mSymbolByUnderline = " + mSymbolByUnderline);

        setInfo(DateUtils.format8(System.currentTimeMillis()) + "   Server : " + symbolId + "  ,  Current : " + mSymbolByUnderline);
        if (!TextUtils.isEmpty(symbolId) && symbolId.equals(mSymbolByUnderline)) {
            LatestDealsAndOutlets.PayloadBean payload = latestDealsAndOutlets.getPayload();
            if (payload == null) {
                return;
            }
            //处理头部数据
            setTopData(payload);
        }
    }

    private void setTopData(final LatestDealsAndOutlets.PayloadBean payload) {
        //LogUtils.i("payload = " + payload.getSymbolId());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WebSocketHelper.getInstance().release();
        WebSocketHelper.getInstance().unRegisterDataReceiveListener(mOnDataReceiveListener);
    }
}
